import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,propConfig,parseToTopbar} from '../apis/core-util'


export default class MainScope extends Scope {
  constructor(){
    super();
    this.$state = {
      viewKey: 'avg_resp',
      showWorld:location.hash.indexOf('#/country') !== -1,
      topbar:{
        $id:'topbar1',
        loading:true
      },
      sortOptions:_.chain(propConfig)
      .pick('avg_resp','thp','total_receive_size','session_count','error_rate')
      .map(function(v,k){
        return {
          key:k,
          label:v.label
        }
      }).value()
    };
    this.topbarApi = coreApi({
      limit:20,
      urlName:'httptransaction',
      size:1
    },TimeStore.getValue,() => {
      if(this.$state.showWorld){
        return {
          classify:'country'
        }
      }else{
        return {
          classify:'region',
          countryCodeId:BlueWare.countryId//china
        }
      }
    },() => {
      return {
        viewKey:this.$state.viewKey
      }
    },() => {
      return {order:sortMap[this.$state.viewKey]}
    },() => {
      if(this.$state.viewKey==='avg_resp'||this.$state.viewKey==='thp'){
        return {
          urlName:'httptransaction',
          classifyKeyField:'time',
          classifyCountField:'num'
        }
      }else if(this.$state.viewKey==='total_receive_size'){
        return {
          urlName:'httptransaction',
          classifyKeyField:'receive',
          classifyCountField:'num'
        }
      }else if(this.$state.viewKey==='session_count'){
        return {
          urlName:'session'
        }
      }else if(this.$state.viewKey==='error_rate'){
        return {
          urlName:'errorrate',
          errorCode:0
        }
      }
    });
  }
  fetch(){
    var _this = this
    this.topbarApi.fetch().then((data,params) => {
      var opts = {}

      let parser = {
        itemMapper: item => {
          if (!this.$state.showWorld ||
              (this.$state.showWorld && !isNaN(parseInt(item.alias)))) {
            return {cnCode:item.alias}
          }
          return null;
        },
        parsedItemMapper: item => {
          if (!this.$state.showWorld ||
              (this.$state.showWorld && !isNaN(parseInt(item.cnCode)))) {
            item.routeUrl += '/' + item.cnCode;
          }
          return item;
        }
      };

      if(this.$state.showWorld){
        opts.classifyTree = [{
          key:'country'
        },{
          key:'region',
          requireId:'countryCodeId',
          options:{parser}
        }]
      }
      opts.parser = parser;
      this.$state.topbar = {
        data:parseToTopbar(data,params,opts)
      }
    },function(err){
      _this.$state.topbar = {
        error:err
      }
    },function(params){
      _this.$state.topbar = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
  }
  $settime(payload){
    this.fetch()
  }
  $changesort(payload){
    this.$state.viewKey = payload.key;
    this.fetch()
  }
  $toggle_map(payload){
    this.$state.showWorld = payload.showWorld
    this.emit()
    this.fetch()
  }
  $load(){
    this.fetch()
  }
}
